package com.gs.wechatdemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.gs.wechatdemo.R;
import com.gs.wechatdemo.adapter.ImagePreviewAdapter;
import com.gs.wechatdemo.constant.AllConstants;
import com.gs.wechatdemo.control.ViewPager;
import com.gs.wechatdemo.util.WindowUtil;

import java.util.List;

public class ImagePreviewActivity extends Activity {

    private ViewPager vpImage;
    private List<String> imageList;
    private int mCurrentPosition;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_preview);
        WindowUtil.setBarBgColor(this, R.color.color_4a);
        vpImage = findViewById(R.id.vpImage);
        getIntentData();
        setView();
    }

    private void setView() {
        if (imageList == null) {
            return;
        }
        ImagePreviewAdapter adapter = new ImagePreviewAdapter(this, this, imageList, R.layout.image_preview);
        vpImage.setAdapter(adapter);
        vpImage.setCurrentItem(mCurrentPosition);

        vpImage.addOnPageChangeListener(new androidx.viewpager.widget.ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurrentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        vpImage.setPageTransformer(false, new androidx.viewpager.widget.ViewPager.PageTransformer() {
            @Override
            public void transformPage(@NonNull View page, float position) {
                float normalizedPosition = Math.abs(Math.abs(position) - 1);
                page.setScaleX(normalizedPosition / 2 + 0.5f);
                page.setScaleY(normalizedPosition / 2 + 0.5f);
            }
        });
    }


    private void getIntentData() {
        if (getIntent() != null) {
            Intent intent = getIntent();
            imageList = (List<String>) intent.getSerializableExtra(AllConstants.LIST_IMG);
            mCurrentPosition = intent.getIntExtra(AllConstants.ID, 0);
        }
    }
}